SetGfxDriver 1:Graphics 1024,768,8,2:SeedRnd MilliSecs() :SetBuffer FrontBuffer() 
Global in1,in2,in3,sort1,sort2,zzz1,error,in4
in1 = CreateBank(200):in2 = CreateBank(200):in3 = CreateBank(200):in4 = CreateBank(200)
Dim decode(10000):error="0"
Dim encode(10000):Dim aaa(60):Dim aaa1(60):Dim aaa2(60):sort1 = CreateBank(60):sort2 = CreateBank(60):Dim zzz(66666)
Dim zzz2(8881,100)
decodet()
While Not KeyHit(1) 
ClsColor 0,0,0
Cls
randnum()
getnums()
;erase()
checkerror()
zzz1=zzz1+1
Flip
If zzz1> 719 Then  WaitMouse() 
VWait 
Wend 

Function erase()
For unittest=0 To 5
	For i = 0 To 9 
	PokeInt in4,i*5, PeekInt (in2,i*5)
	Next 
	cnt00=0
	For hlfaza = 1 To unittest
		For 	y1=0 To 9
		x = PeekInt (in2,y1*5)
		x000 = x
		x =mod2(x Xor (unittest Xor 65534))
		x111= mod2(x Xor (unittest Xor 65534))
;		x =  Rand (0,65535)
		;If x000<>x111 Then PokeInt in2,y1*5,255

PokeInt in4,y1*5,x
Next
Next
eee=fact(fact(PeekInt(in4,0),PeekInt(in4,5),PeekInt(in4,10)),fact(PeekInt(in4,0+15),PeekInt(in4,5+15),PeekInt(in4,10+15)),fact(PeekInt(in4,0+2*15),PeekInt(in4,5+2*15),PeekInt(in4,10+2*15)))
If eee = mod1(unittest*6) Then cnt00=cnt00+1:unt=unittest
Next

If cnt00 = 1 Then
unittest=unt
	For hlfaza = 1 To unittest
		For 	y1=0 To 9
		x = PeekInt (in2,y1*5)
		x =mod2(x Xor (unittest Xor 65534))
;PokeInt in2,y1*5,x
Next
;eee=fact(fact(PeekInt(in4,0),PeekInt(in4,5),PeekInt(in4,10)),fact(PeekInt(in4,0+15),PeekInt(in4,5+15),PeekInt(in4,10+15)),fact(PeekInt(in4,0+2*15),PeekInt(in4,5+2*15),PeekInt(in4,10+2*15)))
;If eee = mod1(unittest*6) Then cnt00=cnt00+1:unt=unittest
Next
EndIf 








End Function

Function fact_faza()
For unittest=0 To 5
	For i = 0 To 9 
	PokeInt in2,i*5, PeekInt (in1,i*5)
	Next 
	initf=fact(fact(PeekInt(in2,0),PeekInt(in2,5),PeekInt(in2,10)),fact(PeekInt(in2,0+15),PeekInt(in2,5+15),PeekInt(in2,10+15)),fact(PeekInt(in2,0+2*15),PeekInt(in2,5+2*15),PeekInt(in2,10+2*15)))
;	initfL=fact(fact(PeekInt(in2,0),PeekInt(in2,0+15),PeekInt(in2,0+2*15)),fact(PeekInt(in2,5),PeekInt(in2,5+15),PeekInt(in2,5+2*15)),fact(PeekInt(in2,10),PeekInt(in2,10+15),PeekInt(in2,10+2*15)))
;	initf= initfH*6+initfL
;	Goto hfaza
	For hlfaza = 1 To unittest
		; ============ LOW FAZA ================

		For 	y1=0 To 9
		x = PeekInt (in2,y1*5)
;		x1 = Int(Floor(x/256))
;		x2 = x-x1*256
;		x1 =mod2(x1 -mod1(hlfaza +unittest) )
;		x2 =mod2(x2 +mod1(hlfaza +unittest))
x000 = x
;		x =x Xor 15
		x111= mod2(x Xor (unittest Xor 65534))
;		x =  Rand (0,65535)
		
;		x = x1*256+x2

;If x000<>x111 Then PokeInt in2,y1*5,255
;
PokeInt in2,y1*5,x
		Next	
	Goto hend	
				; ============= 3-rd FAZA ===============
				x1 = fact(PeekInt(in2,0),PeekInt(in2,5),PeekInt(in2,10))
				x2 = fact(PeekInt(in2,0+15),PeekInt(in2,5+15),PeekInt(in2,10+15))
				x3 = fact(PeekInt(in2,0+2*15),PeekInt(in2,5+2*15),PeekInt(in2,10+2*15))
				x0 = mod3(x1 + x2*6 + x3*36+ unittest);+ unittest*10)
				xx3 = Int(Floor(x0/36))
				x0 = x0  - xx3*36
				xx2 = Int(Floor(x0/6))
				xx1 = x0  - xx2*6
;				DebugLog x1+" "+x2+" "+x3+" "+x4				
;				DebugLog xx1+" "+xx2+" "+xx3+" "+xx4: WaitMouse()
				If xx1 <> x1 Then
					xt1=0
					ttt1$ = decode(xx1*5)
					xa1=Mid$(ttt1$,1,1)
					xa2=Mid$(ttt1$,2,1)
					xa3=Mid$(ttt1$,3,1)
	        		aaa(0)=PeekInt (in2,0):aaa(5)=PeekInt (in2,5):aaa(10)=PeekInt (in2,10)			
					aaa2(0)=aaa(0):aaa2(5)=aaa(5):aaa2(10)=aaa(10)
					For x=3 To 1 Step -1
					maxx =0
					For xt=0 To 2
					xx = aaa(xt*5):If maxx<xx Then maxx=xx:yy2=xt*5
					Next 
					aaa(yy2) =-10:aaa1(x*5)=aaa2(yy2)
					Next							
					PokeInt in2,xt1*15 + 0*5,aaa1(xa1*5)
					PokeInt in2,xt1*15 + 1*5,aaa1(xa2*5)
					PokeInt in2,xt1*15 + 2*5,aaa1(xa3*5)	
;				DebugLog aaa(0)+" "+aaa(5)+" "+aaa(10)+" "+aaa(15)				
;				DebugLog aaa2(0)+" "+aaa2(5)+" "+aaa2(10)+" "+aaa2(15): WaitMouse()									
				EndIf 								
				If xx2 <> x2 Then
					xt1=1
					ttt1$ = decode(xx2*5)
					xa1=Mid$(ttt1$,1,1)
					xa2=Mid$(ttt1$,2,1)
					xa3=Mid$(ttt1$,3,1)					
	        		aaa(0)=PeekInt (in2,xt1*15 + 0):aaa(5)=PeekInt (in2,xt1*15 + 5):aaa(10)=PeekInt (in2,xt1*15 + 10)				
					aaa2(0)=aaa(0):aaa2(5)=aaa(5):aaa2(10)=aaa(10)
					For x=3 To 1 Step -1
					maxx =0
					For xt=0 To 2
					xx = aaa(xt*5):If maxx<xx Then maxx=xx:yy2=xt*5
					Next 
					aaa(yy2) =-10:aaa1(x*5)=aaa2(yy2)
					Next							
					PokeInt in2,xt1*15 + 0*5,aaa1(xa1*5)
					PokeInt in2,xt1*15 + 1*5,aaa1(xa2*5)
					PokeInt in2,xt1*15 + 2*5,aaa1(xa3*5)
							
				EndIf 	
				If xx3 <> x3 Then
					xt1=2
					ttt1$ = decode(xx3*5)
					xa1=Mid$(ttt1$,1,1)
					xa2=Mid$(ttt1$,2,1)
					xa3=Mid$(ttt1$,3,1)

	        		aaa(0)=PeekInt (in2,xt1*15 + 0):aaa(5)=PeekInt (in2,xt1*15 + 5):aaa(10)=PeekInt (in2,xt1*15 + 10)				
					aaa2(0)=aaa(0):aaa2(5)=aaa(5):aaa2(10)=aaa(10)
					For x=3 To 1 Step -1
					maxx =0
					For xt=0 To 2
					xx = aaa(xt*5):If maxx<xx Then maxx=xx:yy2=xt*5
					Next 
					aaa(yy2) =-10:aaa1(x*5)=aaa2(yy2)
					Next							
					PokeInt in2,xt1*15 + 0*5,aaa1(xa1*5)
					PokeInt in2,xt1*15 + 1*5,aaa1(xa2*5)
					PokeInt in2,xt1*15 + 2*5,aaa1(xa3*5)				
				EndIf 									
			.hfaza
		; ============= HIGH FAZA ===============			
		For xt1=0 To 2
			For xt=0 To 2
				PokeInt sort1,xt*5,PeekInt (in2,xt1*15 + xt*5)	
				PokeInt sort2,xt*5,PeekInt (in2,xt1*15 + xt*5)					
			Next		
				For xt=0 To 2
					aaa(xt*5)=PeekInt (sort1,xt*5)+1
				Next
				For x=3 To 1 Step -1
				maxx =0
				For xt=0 To 2
				xx = aaa(xt*5):If maxx<xx Then maxx=xx:yy2=xt*5
				Next 
				aaa(yy2) =-10:aaa1(x*5)=PeekInt (sort2,yy2):PokeInt sort1,yy2,x
				Next
				ttt = Int(""+ PeekInt (sort1,0*5) +""+PeekInt (sort1,1*5) +""+PeekInt (sort1,2*5))
				xx = encode(ttt*5): 
;				DebugLog PeekInt (sort2,0)+" "+PeekInt (sort2,5)+" "+PeekInt (sort2,10)+" " + PeekInt (sort2,15) 
;				DebugLog xx		
				xx = mod1(xx + unittest) 
;				DebugLog xx						
				ttt1$ = decode(xx*5)
				x1=Mid$(ttt1$,1,1)
				x2=Mid$(ttt1$,2,1)
				x3=Mid$(ttt1$,3,1)
;				DebugLog aaa1(5)+" "+aaa1(10)+" "+aaa1(15)+" " + aaa1(20)
;				DebugLog aaa1(x1*5)+" "+aaa1(x2*5)+" "+aaa1(x3*5)+" " + aaa1(x4*5): WaitMouse ()
				PokeInt in2,xt1*15 + 0*5,aaa1(x1*5)
				PokeInt in2,xt1*15 + 1*5,aaa1(x2*5)
				PokeInt in2,xt1*15 + 2*5,aaa1(x3*5)	
		Next	

	Next	
	
	.hend
 				
	eee=fact(fact(PeekInt(in2,0),PeekInt(in2,5),PeekInt(in2,10)),fact(PeekInt(in2,0+15),PeekInt(in2,5+15),PeekInt(in2,10+15)),fact(PeekInt(in2,0+2*15),PeekInt(in2,5+2*15),PeekInt(in2,10+2*15)))
;	initfL=fact(fact(PeekInt(in2,0),PeekInt(in2,0+15),PeekInt(in2,0+2*15)),fact(PeekInt(in2,5),PeekInt(in2,5+15),PeekInt(in2,5+2*15)),fact(PeekInt(in2,10),PeekInt(in2,10+15),PeekInt(in2,10+2*15)))
	;eee = initfH*6+initfL

If eee = mod1(unittest*1) Then zzz(zzz1)=zzz(zzz1)+1:zzz2(zzz1,zzz2(zzz1,100))=unittest:zzz2(zzz1,100)=zzz2(zzz1,100)+1
;If unittest =0 Then initf=eee
If zzz2(zzz1,100)=0 Then zzz2(zzz1,99)=eee
Next

End Function

Function getnums()
	For y=0 To 2
	For x=0 To 2
		Color 0,255,0:Text 30+x*30,y*9+10,PeekInt(in1,5*(y*3+x)),False ,False 
	Next
	Next
	xx1=fact(PeekInt(in1,0),PeekInt(in1,5),PeekInt(in1,10)):Text 150,10,xx1,False ,False 
	xx2=fact(PeekInt(in1,0+15),PeekInt(in1,5+15),PeekInt(in1,10+15)):Text 150,19,xx2,False ,False 
	xx3=fact(PeekInt(in1,0+2*15),PeekInt(in1,5+2*15),PeekInt(in1,10+2*15)):Text 150,28,xx3,False ,False 
	lxx1 = fact(xx1,xx2,xx3)
	Text 160,22,lxx1,False ,False 
	
	xx1=fact(PeekInt(in1,0),PeekInt(in1,0+15),PeekInt(in1,0+2*15)):Text 180,10,xx1,False ,False 
	xx2=fact(PeekInt(in1,5),PeekInt(in1,5+15),PeekInt(in1,5+2*15)):Text 180,19,xx2,False ,False 
	xx3=fact(PeekInt(in1,10),PeekInt(in1,10+15),PeekInt(in1,10+2*15)):Text 180,28,xx3,False ,False 
	lxx2=fact(xx1,xx2,xx3)
	Text 190,22,lxx2,False ,False:Text 210,22,lxx1*6+lxx2,False ,False 

	fact_faza()
	
	For y=0 To 2
	For x=0 To 2
	Color 255,255,0:Text 30+x*30,y*9+50,PeekInt(in2,5*(y*3+x)),False ,False 
	Next
	Next
	xx1=fact(PeekInt(in2,0),PeekInt(in2,5),PeekInt(in2,10))
	xx2=fact(PeekInt(in2,0+15),PeekInt(in2,5+15),PeekInt(in2,10+15))
	xx3=fact(PeekInt(in2,0+2*15),PeekInt(in2,5+2*15),PeekInt(in2,10+2*15))
	lxx1 = fact(xx1,xx2,xx3)
	Text 160,62,lxx1,False ,False 

	xx1=fact(PeekInt(in2,0),PeekInt(in2,0+15),PeekInt(in2,0+2*15))
	xx2=fact(PeekInt(in2,5),PeekInt(in2,5+15),PeekInt(in2,5+2*15))
	xx3=fact(PeekInt(in2,10),PeekInt(in2,10+15),PeekInt(in2,10+2*15))
	lxx2 = fact(xx1,xx2,xx3)
	Text 190,62,lxx2,False ,False :Text 210,62,lxx1*6+lxx2,False ,False 	
	
;         STAT
	
	ttt0=0:wnullmax=0
	For i=0 To zzz1
	If zzz(i) = 0 Then 
		cnull = cnull +1
		If wnullmax < wnullmax0 Then wnullmax = wnullmax0
		wnullmax0=0
	EndIf
	If zzz(i) <> 0 Then wnullmax0=wnullmax0+1
	;If zzz(i) = 0 Then ttt0 = ttt0+1
	ttt0 = ttt0+zzz(i)
	Color 255,255,0:Text 15+(i*12) Mod 960,110+68*Int(i/80),Str(zzz(i))+",",False ,False 
	For i9=0 To zzz2(i,100)-1
	Color zzz2(i,i9)*51,zzz2(i,i9)*51,255:Text 15+(i*12) Mod 960,110+68*Int(i/80)+i9*10+10,Str(zzz2(i,i9))+"",False ,False 	
	Next	
	If zzz2(i,100) = 0 Then Color 255,zzz2(i,99)*51,zzz2(i,99)*51:Text 15+(i*12) Mod 960,110+68*Int(i/80)-7,Str(zzz2(i,99))+"",False ,False 	

    Next
	cnull = Ceil(cnull * 100 /  (zzz1+1))

;	SetFont fnt2
	Color 255,255,210:Text 10,95," middle=" + ttt0+"/"+ zzz1 + ", Null cnt=" + cnull+"%, max without Null="+wnullmax+", phase=3",False ,False
;	SetFont fnt1 
End Function

Function fact(x1,x2,x3)
        aaa(0)=x1+1:aaa(5)=x2+1:aaa(10)=x3+1
	For x=3 To 1 Step -1
	maxx =0
	For xt=0 To 2
	xx = aaa(xt*5):If maxx<xx Then maxx=xx:yy2=xt*5
	Next 
	aaa(yy2) =-10:PokeByte sort1,yy2,x
	Next
	ttt = Int(""+ PeekByte (sort1,0*5) +""+PeekByte (sort1,1*5) +""+PeekByte (sort1,2*5))
	xx = encode(ttt*5)
	Return xx
End Function

Function mod3(x)
While x>215
 x = x -216
Wend 
While  x<0 
 x = x +216
Wend 
Return x
End Function

Function mod2(x)
While x>65535
 x = x -65536
Wend 
While  x<0 
 x = x +65536
Wend 
Return x
End Function

Function mod1(x)
While x>5 
 x = x -6
Wend 
While  x<0 
 x = x +6
Wend 
Return x
End Function

Function randnum()

;If (PeekInt (in1,0) <> 0) Or (PeekInt (in1,5) <> 0) Or (PeekInt (in1,10) <> 0) Or (PeekInt (in1,15) <> 0) Or (PeekInt (in1,20) <> 0) Or (PeekInt (in1,25) <> 0) Or (PeekInt (in1,30) <> 0) Or (PeekInt (in1,35) <> 0) Or (PeekInt (in1,40) <> 0) Then

For i = 0 To 9 
t = Rand(0,65535):PokeInt in1,i*5, t:PokeInt in2,i*5, t:PokeInt in3,i*5, t
;PokeInt in1,i*5, mod2(PeekInt (in1,i*5)  - 1000) :PokeInt in2,i*5, mod2(PeekInt (in2,i*5) -1000)
Next

;Else
;i=0
;PokeInt in1,i*5, 59088 :PokeInt in2,i*5, 59088 : i=i+1
;PokeInt in1,i*5, 60689 :PokeInt in2,i*5, 60689 : i=i+1
;PokeInt in1,i*5, 28870:PokeInt in2,i*5, 28870: i=i+1
;PokeInt in1,i*5, 44562 :PokeInt in2,i*5, 44562 : i=i+1
;PokeInt in1,i*5, 16169 :PokeInt in2,i*5, 16169 : i=i+1
;PokeInt in1,i*5, 51200:PokeInt in2,i*5, 51200: i=i+1
;PokeInt in1,i*5, 17051 :PokeInt in2,i*5, 17051 : i=i+1
;PokeInt in1,i*5, 41239 :PokeInt in2,i*5, 41239 : i=i+1
;PokeInt in1,i*5, 2318:PokeInt in2,i*5, 2318: i=i+1
;EndIf

End Function

Function checkerror()

For i = 0 To 9 
If PeekInt (in2,i*5) <> PeekInt (in3,i*5) Then error = "error.."
Next
	Color 255,0,0:Text 0,0,error,False ,False 
End Function

Function decodet()
y=0
For x1=1 To 3
For x2=1 To 3
For x3=1 To 3
If((x1 <> x2) And (x1<>x3) And (x2<>x3)) decode(y*5) =x1+""+x2+""+x3:encode(Int(x1+""+x2+""+x3)*5)=y:y=y+1
Next
Next
Next
End Function